<template>
  <div class="product-card" @click="goToDetail">
    <div class="product-image">
      <img :src="product.image" :alt="product.name">
      <div class="product-actions">
        <button class="action-btn favorite" @click.stop="handleFavorite">
          <span class="action-icon">{{ isFavorite ? '❤️' : '🤍' }}</span>
        </button>
        <button class="action-btn cart" @click.stop="addToCart">
          <span class="action-icon">🛒</span>
        </button>
      </div>
    </div>
    <div class="product-info">
      <h3 class="product-name">{{ product.name }}</h3>
      <p class="product-desc">{{ product.description }}</p>
      <div class="product-footer">
        <div class="price">¥{{ product.price }}</div>
        <div class="unit">{{ product.unit }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { addToCart as addToCartApi } from '../axios/cart'
import { addFavorite, deleteFavorite } from '../axios/favorite'
import { ElMessage } from 'element-plus'

const router = useRouter()
const props = defineProps({
  product: {
    type: Object,
    required: true
  },
  initialFavorite: {
    type: Boolean,
    default: false
  }
})

const isFavorite = ref(props.initialFavorite)

const handleFavorite = async () => {
  try {
    // 根据当前状态决定是添加还是取消收藏
    const response = isFavorite.value
      ? await deleteFavorite(props.product.id)
      : await addFavorite(props.product.id)

    console.log('收藏操作结果:', response)

    // 检查响应状态
    if (response === true || response === 1 || response.code === 1) {
      isFavorite.value = !isFavorite.value
      ElMessage.success(isFavorite.value ? '已添加到收藏' : '已取消收藏')
    } else {
      ElMessage.error('操作失败')
    }
  } catch (err) {
    console.error('收藏操作失败:', err)
    ElMessage.error('操作失败，请稍后重试')
  }
}

const goToDetail = () => {
  router.push({
    name: 'product-detail',
    params: { id: props.product.id }
  })
}

const addToCart = async (e) => {
  e.stopPropagation()
  try {
    const response = await addToCartApi({
      productId: props.product.id,
      quantity: 1
    })
    if (response.code === 1) {
      ElMessage.success('已添加到购物车')
    } else {
      ElMessage.error(response.msg || '添加失败')
    }
  } catch (err) {
    console.error('添加到购物车失败:', err)
    ElMessage.error('添加失败，请稍后重试')
  }
}
</script>

<style scoped>
.product-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.product-image {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-actions {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 8px;
  opacity: 0;
  transform: translateX(20px);
  transition: all 0.3s ease;
}

.product-card:hover .product-actions {
  opacity: 1;
  transform: translateX(0);
}

.action-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: scale(1.1);
}

.action-btn.favorite:hover {
  background: #ff6b6b;
}

.action-btn.cart:hover {
  background: #4CAF50;
}

.action-icon {
  font-size: 18px;
}

.product-info {
  padding: 20px;
}

.product-name {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 8px 0;
}

.product-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 16px 0;
  line-height: 1.5;
}

.product-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 20px;
  font-weight: 600;
  color: #e74c3c;
}

.unit {
  font-size: 14px;
  color: #666;
}
</style> 